package newcoder_exp.justPractise;

import java.util.*;
import org.junit.*;

import utils.BracketUtils;
public class NC147 {

    public class Solution {

        /*
         * 
         * 2,[[1,2],[2,3]] 复制 返回值： 1 复制 说明： 只需要一个主持人就能成功举办这两个活动
         * 2,[[1,3],[2,4]] 复制 返回值： 2 复制 说明： 需要两个主持人才能成功举办这两个活动
         */
        public int minmumNumberOfHost(int n, int[][] startEnd) {
            if (n == 0) return 0;
            Queue<Integer> queue = new PriorityQueue<>();
            Arrays.sort(startEnd, (n1, n2) -> {
                if (n1[0] != n2[0]) {
                    return n1[0] - n2[0];
                } else {
                    return n1[1] - n2[1];
                }
            });
            for (int i = 0; i < n; i++) {
                //当前开始时间小于队列最早的结束时间，只能多加一个人
                if (queue.isEmpty() || queue.peek() > startEnd[i][0]) {
                    queue.offer(startEnd[i][1]);
                } else {
                    queue.poll();
                    queue.offer(startEnd[i][1]);
                }
            }
            return queue.size();
        }
        class Node implements Comparable<Node> {
            int start;
            int end;
            public Node(int start, int end) {
                this.start = start;
                this.end = end;
            }

            @Override
            public String toString() {
                return "[" + this.start + ", " + this.end + "]";
            }

            @Override
            public int compareTo(Node o) {
                if (this.start == o.start) {
                    return this.end - o.end;
                }
                return this.start - o.start;
            }
        }
    }

    @Test
    public void test() {
        Solution s = new Solution();
        int[][] nums = BracketUtils.to2DArray("[[348,813],[340,385],[465,676],[64,400],[744,959],[130,974],[769,963],[269,901],[406,880],[30,980],[439,854],[451,658],[341,650],[40,796],[332,982],[14,647],[777,787],[614,823],[74,545],[114,199],[406,665],[546,943],[577,806],[640,986],[499,866],[392,554],[388,693],[191,622],[586,997],[114,808],[41,775],[52,884],[184,718],[118,938],[224,498],[78,746],[87,672],[117,216],[66,313],[605,874],[41,876],[269,771],[180,368],[462,677],[185,736],[136,188],[178,821],[195,931]]");
        System.out.println(s.minmumNumberOfHost(48, nums));
    }
}
